<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
require_once("./_inc/crulFunction.php");

$errMsg = "";
$module = sxREQ('mod');
$name = sxREQ('name');
$alias = sxREQ('alias');

if(sxREQ("reqtype") == "delete"){
		$errMsg .= crulAliasDel();
}
$sSQL = "SELECT cra.*, e.description AS e_des  "
	.", a.attribute, a.description AS a_des "
	." FROM com_rule_alias cra " 
	." LEFT JOIN entity e ON e.entity = cra.entity "
	." INNER JOIN attribute a ON a.entity = cra.entity " 
	." WHERE cra.module='".$module."' "
	." AND cra.name='".$name."' "
	." AND cra.com_alias='".$alias."'"
	." ORDER BY a.entity, a.sequence";
$objSDAO->query($sSQL,"crAl");
$rowA = $objSDAO->fetch("crAl");

$sSQL = "SELECT * FROM com_rule_variable " 
	." WHERE module='".$module."' "
	." AND name='".$name."' "
	." AND com_alias='".$alias."'"
	." ORDER BY variable";
$query = $objSDAO->query($sSQL, "crVar");

$aOpz = array("kMenu"=>'pro.cru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));

echo(bodyHTML($aOpz));
echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','save');
echo getHtmlHidden('mod');
echo getHtmlHidden('name');
echo getHtmlHidden('alias');

echo getCrulHeader($module,$name);

$aTipiVar['W'] = 'simple memory variable';
$aTipiVar['X'] = '1 dimension array memory variables';
$aTipiVar['Y'] = '2 dimensions array memory variables';

$aCmd = array();
if (isset($aTipiVar[$alias])) {
	$aCmd["insVar"] = array("pag"=>"crulVarIns.php?pu=".$processUnit."&alias=".$alias,
										"title"=>_t("new variable"),
										"text"=>_t("new variable"),
										"perm"=>"newmod");
} 
if (!isset($aTipiVar[$alias])) {
	$aCmd["mod"] = array("pag"=>"crulAliasMod.php?pu=".$processUnit."&alias=".$alias,
											"title"=>_t("change"),
											"text"=>_t("change"),
											"perm"=>"viewmod");
	$aCmd["del"] = array("pag"=>"javascript:confDel();",
											"title"=>_t("delete"),
											"text"=>_t("delete"),
											"perm"=>"viewmod");
}
$aCmd["back"] = array();
$sCmd = (commandsHTML($aCmd));	
echo $sCmd;

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='4'>Alias</td>" .
		"</tr>\n");
if($rowA['read_only'] == "Y")
	$read_only = "yes";
else
	$read_only = "no";		
if (isset($aTipiVar[$alias])) {
	echo("<tr>" .
			"<td class=\"labl01\" width='10%'>Alias</td>" .
			"<td class=\"text01\" width='70%'>".$alias." - ".$aTipiVar[$alias]."</td>" .
			"<td class=\"labl01\" width='10%'>Read only</td>" .
			"<td class=\"text01\" width='10%'>$read_only</td>" .
		"</tr>\n");
} else {
	echo("<tr>" .
			"<td class=\"labl01\" width='10%'>Alias</td>" .
			"<td class=\"text01\" width='70%'>".$alias." - ".$rowA['entity']."</td>" .
			"<td class=\"labl01\" width='10%'>Read only</td>" .
			"<td class=\"text01\" width='10%'>$read_only</td>" .
	"</tr>\n");
	echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\" colspan='3'>".sxHE(ucfirst($rowA['e_des']).
		((empty($rowA['description']))?'':" - ".$rowA['description']))."</td>" .
		"</tr>\n");
}
if (!empty($rowA['note'])) {
	echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\"><pre class=\"text01\">".sxWW(sxHE($rowA['note']))."</pre></td>" .
		"</tr>\n");
}
echo("</table>");
if ($alias > 'Q') {
	echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
	echo("<tr>" .
			"<td class=\"titl01\"colspan=\"3\">List of variables</td>" .
		"</tr>\n");
	echo("<tr>" .
			"<td class=\"tabh01\" width=\"18%\">Name</td>" .
			"<td class=\"tabh01\" width=\"2%\">R/O</td>" .
			"<td class=\"tabh01\" width=\"80%\">Description</td>" .
		"</tr>\n");	
	while($row = $objSDAO->fetch("puVar")){
		echo("<tr>" .
				"<td class=\"text01\" valign='top'><a class=\"text01\" ".
					goToPag("puVarView.php?pu=".$row["process_unit"]."&alias=".$row["alias"]."&var=".$row["variable"]."&xsn=".XSN).
					" title=\""._t("Visualizza variabile")."'".sxHE($row["description"])."'\">".
					$row["alias"].'_'.sxHE($row["variable"])."</a></td>\n" .
				"<td class=\"text01\" valign='top'>".(($row["read_only"]=='Y')?'yes':'no')."</td>" .
				"<td class=\"text01\" valign='top'>".sxHE($row["description"])."</td>" .
			"</tr>\n");
	}
	echo("</table>\n");
}
if ($alias < 'W') {
	$numElem = 0;
	//list entity-attribute
	echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
	echo("<tr>" .
			"<td class=\"titl01\"colspan=\"4\">"._t("Lista attributi")."</td>" .
		"</tr>\n");
	echo("<tr>" .
			"<td class=\"tabh01\" width=\"19%\">Attribute</td>" .
			"<td class=\"tabh01\" width=\"1%\" align='center'>Read-only</td>" .
			"<td class=\"tabh01\" width=\"50%\">Description</td>" .
		"</tr>\n");	
	while($row = $objSDAO->fetch("puAttr")){
		if ($row['pat_attr'] == '') {
			$bi = '<i>'; $ei = '</i>';
		} else {
			$bi = $ei = '';
		}
		echo("<tr>" .
			"<td class=\"text01\" valign='top'>$bi<a class=\"text01\" ".
				goToPag("puAttrView.php?pu=".$row["process_unit"]."&alias=".$row["alias"]."&ent=".$row["entity"]."&attr=".$row["attribute"]."&xsn=".XSN).
				" title=\""._t("Visualizza attribute")."'".sxHE($row["description"])."'\">".
				sxHE($row["alias"])."_" .$row["attribute"]."</a>$ei</td>\n");
		echo("<td class=\"text01\" valign='top' align='center'>".(($row["read_only"]=='Y')?'R/O':'&nbsp;')."</td>" .
				"<td class=\"text01\" valign='top'>".sxHE($row["description"])."</td>" .
			"</tr>\n");
	}
	echo("</table>\n");
}

echo $sCmd;

echo("</td>\n</tr>\n</table>\n");
echo("</form>");
echo(footerHTML());
exit();

function puAliasDel(){
 	global $objSDAO;
	$errMsg = "";
	$sSQL = "DELETE FROM pu_variable " .
		"WHERE process_unit='".sxREQ('pu')."' AND alias='".sxREQ('alias')."'";	
	$objSDAO->query($sSQL,"del");
	
	$sSQL = "DELETE FROM pu_attribute " .
		"WHERE process_unit='".sxREQ('pu')."' AND alias='".sxREQ('alias')."'";	
	$objSDAO->query($sSQL,"del");
	
	$sSQL = "DELETE FROM frame_alias_match " .
		"WHERE process_unit='".sxREQ('pu')."' AND alias='".sxREQ('alias')."'";	
	$objSDAO->query($sSQL,"del");
	
	$sSQL = "DELETE FROM pu_alias " .
		"WHERE process_unit='".sxREQ('pu')."' AND alias='".sxREQ('alias')."'";	
	$objSDAO->query($sSQL,"del");
	
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return 'abnormal end';
} 
